﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="get_report_average_score.aspx.cs" Inherits="SQT.tracnghiem.Ajax.ASP.Class.get_report_average_score" %>

<%@ Import Namespace="DataAccess" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<body>

    <%
        int id = int.Parse(Request.QueryString["id"]);
        var get_semester = new TblSemesterController().FetchAll().Single(x => x.SemesterId == id);

        int cls = int.Parse(Request.QueryString["cls"]);
        var get_cls = new TblClassController().FetchAll().Single(x => x.Id == cls);
        var get_teacher = new TblManagerController().FetchAll().Single(x => x.ManagerId == get_cls.Teacher);
        string[] name = new string[50];
        int[] average = new int[50];
        int siso = 0;
        name[0] = "Chuẩn";
        average[0] = 100;
        var get_group = new TblExamGroupController().FetchAll().Where(x => x.Semester == get_semester.SemesterId);
        int count = 0;
        foreach (TblExamGroup group in get_group)
        {
            var get_exam = new TblExamController().FetchAll().Where(x => x.ExamGroup == group.GroupId);
            foreach (TblExam exam in get_exam)
            {
                count++;
                name[count] = exam.ExamName.ToString();

                decimal? average_temp = 0;
                var get_student = new TblUserController().FetchAll().Where(x => x.ClassX == cls && x.Active == true);
                siso = get_student.ToList().Count;
                foreach (TblUser user in get_student)
                {
                    var get_score = new TblExamScoreController().FetchAll().Where(x => x.ScoreExam == exam.ExamId && x.ScoreStudentId == user.UserId);

                    foreach (TblExamScore score in get_score)
                    {
                        if (id != 3)
                        {
                            if (score.ScoreWrite == null || score.ScoreGrammar == null || score.ScoreListen == null || score.ScoreKanji == null)
                            {
                                average_temp = 0;
                            }
                            else
                            {
                                average_temp += (score.ScoreWrite + score.ScoreGrammar + score.ScoreListen + score.ScoreKanji) / 4;
                            }
                        }
                        else
                        {
                            if (score.ScoreWrite == null || score.ScoreSpeak == null || score.ScoreListen == null || score.ScoreKanji == null)
                            {
                                average_temp = 0;
                            }
                            else
                            {
                                average_temp += (score.ScoreWrite + score.ScoreSpeak + score.ScoreListen + score.ScoreKanji) / 4;
                            }
                        }
                    }
                }
                if (get_student.ToList().Count == 0)
                    continue;

                else
                    average[count] = (int)(average_temp/get_student.ToList().Count);
            }
        }
    %>
    <div id="report_name">
        <%for (int i = 0; i <= count; i++)
            { %>
        <div class="report_name_child"><%=name[i] %></div>
        <%} %>
    </div>
    <div id="report_data">
        <%for (int i = 0; i <= count; i++)
            {%>
        <div class="report_data_child"><%=average[i] %></div>
            <%} %>
    </div>
    <div id="report_text">BẢNG THỐNG KÊ ĐIỂM TRUNG BÌNH CỦA LỚP <%=get_cls.ClassName %> (Sĩ số: <%=siso %>) - Chủ nhiệm: <%=get_teacher.FullName %></div>
</body>
</html>
